package com.zy.algorithm.controller.screen;

import static android.view.View.GONE;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zy.algorithm.R;
import com.zy.algorithm.bean.SortStepBean;
import com.zy.algorithm.controller.IndexController;
import com.zy.algorithm.controller.data.DataAndColorController;
import com.zy.algorithm.curtain.BaseChildScreenBean;
import com.zy.algorithm.curtain.BaseSortScreenBean;
import com.zy.algorithm.curtain.IScreenBean;
import com.zy.algorithm.fragment.BaseAlgorithmFragment;
import com.zy.utils.ActivityUtils;
import com.zy.utils.AnimatorUtils;
import com.zy.zlog.ZLog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//首个屏幕-动画控制器
public class BaseBallFirstScreenController extends BaseScreenController {

    private static final String TAG = "FirstScreenController";

    //private final TextView[] dataTVS = new TextView[10];
    private Map<Integer, Integer> colorMap = new ConcurrentHashMap<>();

    private final Context context;
    private final boolean colorAni;
    protected final Resources resources;

    //PK 区域View组件
    protected TextView pk_first;
    protected TextView pk_second;
    protected TextView pk_op;

    protected IndexController indexController;
    protected DataAndColorController dataController;

    LinearLayout base_data_container_V2;

    List<TextView> textViewList = new ArrayList<>();


    public BaseBallFirstScreenController(View root, boolean colorAni, IndexController indexController) {
        super(root.getContext());
        context = root.getContext();
        resources = context.getResources();
        this.colorAni = colorAni;
        this.indexController = indexController;

        initDataTV(root);
        initPKView(root);
    }

    //初始化PK 区域
    private void initPKView(View view) {
        pk_first = view.findViewById(R.id.pk_first);
        pk_second = view.findViewById(R.id.pk_second);
        pk_op = view.findViewById(R.id.pk_op);
    }

    private void initDataTV(View view) {
        base_data_container_V2 = view.findViewById(R.id.base_data_container_V2);

        dataController = new DataAndColorController(textViewList, resources, colorAni);
    }

    @Override
    protected View[] getDataViewS() {
        View[] views = new View[textViewList.size()];
        return textViewList.toArray(views);
    }

    @Override
    View getDataViewByIndex(int index) {
        return textViewList.get(index);
    }

    public void setPKShow(boolean firstShow, boolean secondShow, boolean opShow) {
        pk_first.setVisibility(firstShow ? View.VISIBLE : GONE);
        pk_second.setVisibility(secondShow ? View.VISIBLE : GONE);
        pk_op.setVisibility(opShow ? View.VISIBLE : GONE);
    }

    /**
     * 只有BaseScreen才调用该方法，设置当前Screen的数据
     * 调用时机：Screen start或者Screen End
     *
     * @param bean
     * @param start
     */
    public void setScreenData(IScreenBean bean, boolean start) {
        checkInit(bean);
        dataController.setScreenData(bean, start);
    }


    /**
     * base Screen的 Screen data.size == SubSet data.size
     * @param bean
     * @param start
     */
    @Override
    public void setSortData(SortStepBean bean, boolean start) {
        dataController.setSortData(bean, start);
    }

    @Override
    int getWH() {
        return context.getResources().getDimensionPixelSize(R.dimen.sort_data_wh);
    }

    @Override
    float getTextSize() {
        return 18;
    }

    boolean init = false;

    private void checkInit(IScreenBean bean) {
        if (!init) {
            init = true;
            initScreenBean(bean);
        }
    }

    public void initScreenBean(IScreenBean bean) {
        int[] subSetStart = bean.getSortScreenStart();
        for (int index = 0; index < subSetStart.length; index++) {
            TextView textView = buildDataTextView(subSetStart[index]);
            base_data_container_V2.addView(buildEmptyView());
            base_data_container_V2.addView(textView);
            textViewList.add(textView);

            if (index == subSetStart.length - 1) {
                base_data_container_V2.addView(buildEmptyView());
            }
        }
    }

    public int getCenterXByIndex(int index) {
        return AnimatorUtils.getGlobalVisibleRect(getDataViewByIndex(index)).centerX();
    }


    protected void showPK(SortStepBean curStepBean, BaseAlgorithmFragment.StepListener listener) {
        //当前比较的数字View
        TextView originFirstTV = (TextView) getDataViewByIndex(curStepBean.getOpFirstIndex());
        TextView originSecondTV = (TextView) getDataViewByIndex(curStepBean.getOpSecondIndex());


        Rect originFirstTVRect = AnimatorUtils.getGlobalVisibleRect(originFirstTV);
        Rect originSecondTVRect = AnimatorUtils.getGlobalVisibleRect(originSecondTV);
        Log.d(TAG, "originFirstTVRect:" + originFirstTVRect);
        Log.d(TAG, "originFirstTVRect:" + originSecondTVRect);


        AnimatorUtils.move(originFirstTV, pk_first);
        AnimatorUtils.move(originSecondTV, pk_second, new AnimatorUtils.AnimationListener() {
            @Override
            public void onAnimationEnd() {
                if (ActivityUtils.checkActivityDestroyed(context)) {
                    return;
                }

                //比较大小
                if (curStepBean.isResult()) {
                    AnimatorUtils.shake(originFirstTV, new AnimatorUtils.AnimationListener() {
                        @Override
                        public void onAnimationEnd() {
                            super.onAnimationEnd();
                            if (ActivityUtils.checkActivityDestroyed(context)) {
                                return;
                            }


                            AnimatorUtils.move(originFirstTV, originSecondTVRect);
                            AnimatorUtils.move(originSecondTV, originFirstTVRect, new AnimatorUtils.AnimationListener() {
                                @Override
                                public void onAnimationEnd() {
                                    super.onAnimationEnd();
                                    listener.nextStep();
                                }
                            });
                        }
                    });
                } else {
                    AnimatorUtils.shake(originSecondTV, new AnimatorUtils.AnimationListener() {
                        @Override
                        public void onAnimationEnd() {
                            super.onAnimationEnd();
                            if (ActivityUtils.checkActivityDestroyed(context)) {
                                return;
                            }

                            AnimatorUtils.reset(originFirstTV);
                            AnimatorUtils.reset(originSecondTV, new AnimatorUtils.AnimationListener() {
                                @Override
                                public void onAnimationEnd() {
                                    super.onAnimationEnd();
                                    listener.nextStep();
                                }
                            });
                        }
                    });
                }
            }
        });
    }

    public void startSubCartoon(List<SortStepBean> cartoonStepBeanList, BaseAlgorithmFragment.StepListener stepListener) {
        //设置当前操作位置
        //一步一步执行

        startPlay(cartoonStepBeanList, 0, stepListener);
    }

    private void startPlay(List<SortStepBean> cartoonStepBeanList, int index, BaseAlgorithmFragment.StepListener stepListener) {
        if (index >= cartoonStepBeanList.size()) {
            stepListener.nextStep();
            return;
        }

        SortStepBean bean = cartoonStepBeanList.get(index);
        ZLog.d(TAG, index + " :" + bean);

        setSortData(bean, true);
        //开始每一步的动画
        playStepAnimation(index, bean, new BaseAlgorithmFragment.StepListener() {
            @Override
            public void nextStep() {

                if (checkActivityDestroyed()) {
                    return;
                }
                statuesResetAll();
                setSortData(bean, false);
                startPlay(cartoonStepBeanList, index + 1, stepListener);
            }
        });
    }

    protected void setOpIndexStatues(SortStepBean bean) {
        dataController.setDataItemRed(bean.getOpFirstIndex());
        dataController.setDataItemRed(bean.getOpSecondIndex());
    }


    protected void playStepAnimation(int index, SortStepBean bean, BaseAlgorithmFragment.StepListener stepListener) {
        if (checkActivityDestroyed()) {
            return;
        }

        //设置当前操作位置
        setOpIndexStatues(bean);

        //下标动画
        indexController.sortIndexAnimation(bean, new BaseAlgorithmFragment.AnimationListener() {
            @Override
            public void onAnimationEnd() {
                if (checkActivityDestroyed()) {
                    return;
                }
                //数据动画
                sortDataAnimation(bean, stepListener);
            }
        });
    }

    private void sortDataAnimation(SortStepBean curStepBean, BaseAlgorithmFragment.StepListener listener) {
        if (checkActivityDestroyed()) {
            return;
        }
        //串行操作
        showPK(curStepBean, listener);
    }

    public List<View> getSubSetIndexListView(List<Integer> subSetIndexList) {
        List<View> originList = new ArrayList<>();
        for (Integer index : subSetIndexList) {
            originList.add(getDataViewByIndex(index));
        }
        return originList;
    }
}
